package com.zr.d_c.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.dao.BookDao;
import com.zr.d_c.daoimpl.BookDaoImpl;
import com.zr.d_c.model.Dish;
import com.zr.d_c.model.PageBean;
import com.zr.d_c.model.Seller;
import com.zr.d_c.service.BookService;
import com.zr.d_c.serviceimpl.BookServiceImpl;
import com.zr.d_c.util.PagingUtil;
/**
 * 展示所选餐厅对应的菜单
 * @author Administrator
 *
 */
public class BookDishController extends HttpServlet{

	BookDao bd = new BookDaoImpl();
	BookService bs = new BookServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		int sid = Integer.parseInt(req.getParameter("sid"));
		req.setAttribute("sid", sid);
		String key = req.getParameter("dishname");
		if (key == null) {
			String p = req.getParameter("page");
			int page = 0;
			if (p == null) {
				page = 1;
			} else {
				page = Integer.parseInt(p);
			}
			PageBean pb = new PageBean(page, 10);
			// 动态生产分页信息
			req.setAttribute("pagedish", PagingUtil.getDishPage(bd.selectDishCount(sid), page, pb.getPageSize(),sid));
			List<Dish> dishs = bs.getDish(pb.getStart(), pb.getPageSize(),sid);
			req.setAttribute("dishs", dishs);
		}else{
			String p = req.getParameter("page");
			int page = 0;
			if (p == null) {
				page = 1;
			} else {
				page = Integer.parseInt(p);
			}
			PageBean pb = new PageBean(page, 10);
			req.setAttribute("pagedish", PagingUtil.getDishPageByKey(bd.selectDishCount(sid,key), page, pb.getPageSize(),key,sid));
			List<Dish> dishs = bs.getDish(pb.getStart(), pb.getPageSize(),sid, key);
			req.setAttribute("dishs", dishs);
		}
		// 获取地址
		req.getRequestDispatcher("restaurant/showdish.jsp").forward(req, resp);
	}
}
